package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.action.CoreController;
import com.base.dao.model.Grid;
import com.base.dao.model.Ret;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;
import com.item.util.QueryParams;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.Principal;
import com.zch.dao.model.ZxMessage;
import com.zch.daoEx.model.PrincipalEx;
import com.zch.daoEx.model.ZxMessageEx;
import com.zch.service.PrincipalService;

@RequestMapping("principal")
@Controller
@LoginFilter
public class PrincipalController extends CoreController{

	@Autowired
	private PrincipalService principalService;
	
	@Autowired
    private MessageService messageService;


	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows,String name,Integer isVerify){
		PaginationSupport.byPage(page, rows);
		QueryParams.Builder builder = QueryParams.newBuilder();
		if (StringUtils.isNotBlank(name)){
			builder.put("name", "%"+name+"%");
		}
		if (isVerify != null && isVerify != 0){
			builder.put("isVerify", isVerify);
		}
		List<PrincipalEx> list = principalService.selectList(builder);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Principal principal){
		if (StringUtils.isBlank(principal.getId())){
			principalService.insert(principal);
		}else{
			int result = principalService.updateByPrimaryKeySelective(principal);
			
			if(result==1){
				principal= principalService.selectByPrimaryKey(principal.getId());
				if(principal.getIsVerify()==2){
	                String pushContent = "审核未通过!理由:"+principal.getNotAgreeReason();
	                //推送
	                messageService.findMessage(principal.getOrganizationName(), pushContent, principal.getUserId(), 1);
	            }else if(principal.getIsVerify()==1){
	            	 String pushContent = "审核通过!";
		                //推送
		                messageService.findMessage(principal.getOrganizationName(), pushContent, principal.getUserId(), 1);
	            }
			}
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Principal principal = principalService.selectByPrimaryKey(id);
		principal.setOrganizationInfo(principal.getOrganizationInfo().replaceAll("\r\n", "<br/>"));
		principal.setInfo(principal.getInfo().replaceAll("\r\n", "<br/>"));
		return JSONUtils.serialize(principal);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        Principal principal = principalService.selectByPrimaryKey(id);
        principal.setOrganizationInfo(principal.getOrganizationInfo().replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        principal.setInfo(principal.getInfo().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(principal);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			principalService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
    /*@RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows,String name,Integer isVerify){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        QueryParams.Builder builder = QueryParams.newBuilder();
        if (StringUtils.isNotBlank(name)){
            builder.put("name", "%"+name+"%");
        }
        if (isVerify != null && isVerify != 0){
            builder.put("isVerify", isVerify);
        }
        builder.put("id", id);
        List<PrincipalEx> list = principalService.selectMyList(builder);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }*/	
	@RequestMapping("/myList")
    @ResponseBody
    public String mylist(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        List<PrincipalEx> list = principalService.selectMyList(map);

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }

	@RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Principal principal){
	    principal.setUserId(getSessionParameter("id"));
	    if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(principal.getId())){
            principalService.insert(principal);
        }else{
            principalService.updateByPrimaryKeySelective(principal);
        }
        return JSONUtils.serialize(new Ret(0));
    }
	
	@RequestMapping("/top")
    @ResponseBody
    public String top(Principal principal){
        if (StringUtils.isBlank(principal.getId())){
            principal.setPublishTime(new Date());
            principalService.insert(principal);
        }else{
            principal.setPublishTime(new Date());
            principalService.updateByPrimaryKeySelective(principal);
        }
        return JSONUtils.serialize(new Ret(0));
    }
	
	@RequestMapping("/cancelTops")
    @ResponseBody
    public String cancelTops(Principal principal){
        if (StringUtils.isBlank(principal.getId())){
            principal.setPublishTime(null);
            principalService.insert(principal);
        }else{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", principal.getId());
            int result = principalService.cancleTops(map);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}